package com.tlxch.task;

import com.tlxch.mapper.SAttitudeMapper;
import com.tlxch.mapper.SEffectMapper;
import com.tlxch.mapper.SHabitMapper;
import com.tlxch.pojo.SAttitude;
import com.tlxch.pojo.SEffect;
import com.tlxch.pojo.SHabit;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;

@Component
@Slf4j
public class SynthesisInfoTask {

    @Autowired
    SAttitudeMapper sAttitudeMapper;
    @Autowired
    SEffectMapper sEffectMapper;
    @Autowired
    SHabitMapper sHabitMapper;

    @Scheduled(cron = "0/20 * * * * ? ")
    public void synthesis() {
        LocalDateTime threeDaysAgo = LocalDateTime.now().minusDays(180);
        List<SAttitude> sAttitudes = sAttitudeMapper.getsAttitude();
        List<SEffect> sEffects = sEffectMapper.getsEffect();
        List<SHabit> sHabits = sHabitMapper.getsHabit();
        for (SAttitude sAttitude : sAttitudes) {
            if (sAttitude.getSaCreatetime().isBefore(threeDaysAgo)){
                //删除
                sAttitudeMapper.deleteByPrimaryKey(sAttitude.getSaId());
            }
        }
        for (SEffect sEffect : sEffects) {
            if (sEffect.getSeCreatetime().isBefore(threeDaysAgo)){
                //删除
                sEffectMapper.deleteByPrimaryKey(sEffect.getSeId());
            }
        }
        for (SHabit sHabit : sHabits) {
            if (sHabit.getShCreatetime().isBefore(threeDaysAgo)){
                //删除
                sHabitMapper.deleteByPrimaryKey(sHabit.getShId());
            }
        }
    }
}
